package com.keqing.tomato.fragment;

import static com.keqing.tomato.util.TimeUtil.compareSameDate;
import static com.keqing.tomato.util.TimeUtil.getStringByFormat;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.keqing.tomato.R;
import com.keqing.tomato.adapter.HistoryItemAdapter;
import com.keqing.tomato.bean.ChildInfo;
import com.keqing.tomato.bean.GroupInfo;
import com.keqing.tomato.bean.ItemInfo;
import com.keqing.tomato.database.TomatoHistory;
import com.keqing.tomato.util.TimeUtil;

import org.litepal.LitePal;

import java.nio.file.FileStore;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class FragmentHistory extends Fragment {
    private RecyclerView recycler_view;
    private View tvEmptyData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("com/keqing/tomato", "FragmentHistory - onCreateView()");
        return inflater.inflate(R.layout.fragment_history, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e("com/keqing/tomato", "FragmentHistory - onViewCreated()");
        recycler_view = view.findViewById(R.id.recycler_view);
        tvEmptyData = view.findViewById(R.id.empty_data);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            Log.e("com/keqing/tomato", "FragmentHistory - onHiddenChanged() : show ....");
            loadData();
        }
    }

    private void loadData() {
        //时间降序list
        List<TomatoHistory> histories = LitePal.select("id", "duration", "title", "startTime", "endTime")
                .order("startTime desc")
                .find(TomatoHistory.class);


//        List<TomatoHistory> histories = new ArrayList<>();
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 25), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 24), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 24), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));

        List<ItemInfo> list = new ArrayList<>();
        int len = histories.size();
        int firstRow = 0;
        while (firstRow <= len - 1){
            Date firstDate = histories.get(firstRow).getStartTime();
            int durationSum = 0;
            int historyCount = 0;
            GroupInfo group = new GroupInfo();
            list.add(new ItemInfo(ItemInfo.type_group, group));
            for (int j = firstRow; j < len ; j++){
                TomatoHistory child = histories.get(j);
                if (compareSameDate(firstDate, child.getStartTime())) {
                    list.add(new ItemInfo(ItemInfo.type_child, transTomatoHistory(child)));
                    durationSum += child.getDuration();
                    historyCount++;
                    if (j == len - 1) {
                        group.changeValue(firstDate,durationSum,historyCount);
                        firstRow = len;
                        break;
                    }
                } else {
                    group.changeValue(firstDate,durationSum,historyCount);
                    firstRow = j;
                    break;
                }
            }
        }


        if (list.size() == 0) {
            tvEmptyData.setVisibility(View.VISIBLE);
        } else {
            tvEmptyData.setVisibility(View.GONE);
        }
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        recycler_view.setLayoutManager(manager);
        HistoryItemAdapter adapter = new HistoryItemAdapter(list);
        recycler_view.setAdapter(adapter);
    }

    private ChildInfo transTomatoHistory(TomatoHistory tomato) {
        return new ChildInfo(
                tomato.getTitle(),
                tomato.getDuration(),
                getStringByFormat(tomato.getStartTime(), TimeUtil.dateFormatHM),
                getStringByFormat(tomato.getEndTime(), TimeUtil.dateFormatHM)
        );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("com/keqing/tomato", "FragmentHistory - onDestroy()");
    }
}